#include <stdlib.h>

#include "YXLog.h"
#include "YXMalloc.h"

static size_t used_memory = 0;

static void (*yx_malloc_oom_handler)(void) = yx_malloc_oom_default_handler;

void yx_malloc_oom_default_handler() {
    logError("Out of memory");
    abort();
}

void *yx_malloc(size_t size) {
    void *ptr = malloc(size);
    if (ptr == NULL)
        yx_malloc_oom_handler();
    used_memory += size;
    return ptr;
}

void *yx_realloc(void *ptr, size_t size) {
    void *new_ptr = realloc(ptr, size);
    if (new_ptr == NULL)
        yx_malloc_oom_handler();
    return new_ptr;
}

size_t yx_used_memory() { return used_memory; }
